﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wd_23_await_v1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public partial class App : Application
        {
            public static log4net.ILog oplog = log4net.LogManager.GetLogger("logger2022");
        }

        public MainWindow()
        {
            InitializeComponent();
            App.oplog.Info("this is error show log.");
        }

        private static bool is_enable = false;
        //private void Button_Click_await(object sender, RoutedEventArgs e)
        private async void Button_Click_await(object sender, RoutedEventArgs e)
        {
            App.oplog.Info("this is error show log.22222");
            awaitbtn.IsEnabled = is_enable;
            
            string s1 = await countCharacters();
            MessageBox.Show(s1);

            awaitbtn.IsEnabled = !is_enable;
        }

        //C# await
        //https://blog.csdn.net/u012338130/article/details/104135640
        private async Task<string> countCharacters()
        {
            return await Task<string>.Run(() => {
                Thread.Sleep(3000);
                
                //awaitbtn.IsEnabled = true;  //不可访问UI
                return "41608617";
            });
        }

    }

}
